﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QUT.Bio.Util;

// TODO: WPF Clipboard

namespace QUT.Bio.SilverMap {
	public class Status : INotifyPropertyChanged {
		private Status() {
		}

		private string message;
		public event PropertyChangedEventHandler PropertyChanged;
		private PopupWindow window;

		private static Status instance = new Status();

		public static Status Instance {
			get {
				return instance;
			}
		}

		public String Message {
			get {
				return message;
			}
			private set {
				message = value;
				if ( PropertyChanged != null ) {
					PropertyChanged( this, new PropertyChangedEventArgs( "Message" ) );
				}
			}
		}

		public static void WriteLine(
			string format,
			params object[] args
		) {
			instance.Message = String.Format(
				"{0}{1}\n",
				instance.message,
				String.Format( format, args )
			);
		}

		public PopupWindow Window {
			get {
				if ( window == null ) {
					window = new PopupWindow() {
						Title = "Status"
					};

					TextBlock tb = new TextBlock() {
						DataContext = instance
					};

					tb.SetBinding( TextBlock.TextProperty, new System.Windows.Data.Binding( "Message" ) );

					ScrollViewer sv = new ScrollViewer() {
						HorizontalAlignment = HorizontalAlignment.Stretch,
						HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
						VerticalAlignment = VerticalAlignment.Stretch,
						VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
						Content = tb
					};
					
					StackPanel bp = new StackPanel() {
						Orientation = Orientation.Horizontal,
						HorizontalAlignment = HorizontalAlignment.Center
					};
					
					#if SILVERLIGHT
					Button copyButton = new Button() {
						Content = "Copy",
						Width = 60,
						Margin = new Thickness( 0, 3, 0, 3 )
					};
					
					copyButton.Click += delegate ( object sender, RoutedEventArgs args ) {
						ClipboardDemo.Clipboard.SetClipboardText( Status.Instance.Message );
					};
					
					bp.Children.Add( copyButton );
					#endif
					
					Button clearButton = new Button() {
						Content = "Clear",
						Width = 60,
						Margin = new Thickness( 5, 3, 0, 3 )
					};
					
					clearButton.Click += delegate ( object sender, RoutedEventArgs args ) {
						Status.Instance.Message = "";
					};

					bp.Children.Add( clearButton );

					window.ClientArea.Centre = sv;
					window.ClientArea.South = bp;
				}
				
				return window;
			}
		}
	}
}
